package com.example.samplethreadtest;

import java.util.logging.Handler;
import java.util.logging.LogRecord;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView countView;
	
	private final static String TAG = "mainActivity";
	
	Handler mHandler = new Handler(){
	Handler PostHandler = new Handler(){
		
	
		@Override
		public void close() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void flush() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void publish(LogRecord record) {
			// TODO Auto-generated method stub
			
		}
		
		
 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		countView = (TextView)findViewById(R.id.countView);
		Button btn=(Button)findViewById(R.id.startCount);
		btn.setOnClickListener(new View.OnClickListener() {
						
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread(new Runnable(){
					private String tag;

					@Override
					public void run() {
						// TODO Auto-generated method stub
					int count = 0;
					while(count<20){
						try{
							Thread.sleep(1000);
							count++;
							countView.setText("count : " +count);
						}catch (Exception e){
//							e.printStackTrace();
						}
						count++;
						Log.i(tag, "count : " + count);
						//	countView.setText("count : " + count);
//						mHandler.sendMessage(
//								mHandler.obtainMessage(MyHandler.MESSAGE_TYPE_TEXTVIEW_UPDATE,"count : "+ count));
						
						mPostHandler.post(new countRunnable(count));
					}
					
					}
				}).start();
			}
		});
	}

	class CountRunnable implements Runnable(){
		int mCount;
		public CountRunnable(int count){
			mCount = count;
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
